//
//  UIImage+Extension.swift
//  SinaImiate
//
//  Created by xiaoming on 16/7/5.
//  Copyright © 2016年 GoodJob. All rights reserved.
//

import Foundation

extension UIImage {
    /// 创建头像图像
    func cz_avatarImage(size: CGSize?, backColor: UIColor = UIColor.white(), lineColor: UIColor = UIColor.lightGray()) -> UIImage? {
        
        var size = size
        
        if size == nil || size?.width == 0{
            
            size = self.size
        }
        
        let rect = CGRect(origin: CGPoint(), size: size!)
        
        UIGraphicsBeginImageContextWithOptions(rect.size, true, 0)
        
        backColor.setFill()
        UIRectFill(rect)
        
        let path = UIBezierPath(ovalIn: rect)
        path.addClip()
        
        draw(in: rect)
        
        let ovalPath = UIBezierPath(ovalIn: rect)
        ovalPath.lineWidth = 2
        lineColor.setStroke()
        ovalPath.stroke()
        
        let result = UIGraphicsGetImageFromCurrentImageContext()
        
        UIGraphicsEndImageContext()
        
        return result
    }
    
    func cz_image(size: CGSize? = nil, backColor: UIColor = UIColor.white()) -> UIImage? {
        
        var size = size
        if size == nil {
            size = self.size
        }
        
        let rect = CGRect(origin: CGPoint(), size: size!)
        
        UIGraphicsBeginImageContextWithOptions(rect.size, true, 0)
        
        backColor.setFill()
        UIRectFill(rect)
        
        draw(in: rect)
        
        let result = UIGraphicsGetImageFromCurrentImageContext()
        
        UIGraphicsEndImageContext()
        
        return result
        
    }
    
}
